package com.luyang.platform.contact.rest.user;

import com.luyang.platform.contact.application.user.UserApplicationService;
import com.luyang.platform.contact.rest.user.convert.UserRequestConverter;
import com.luyang.platform.contact.rest.user.request.CreateUserReq;
import com.luyang.platform.contact.rest.user.response.CreateUserResp;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户管理 web 接口
 *
 * @author lu_yang
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserRestController {

    private final UserRequestConverter userRequestConverter;
    private final UserApplicationService userApplicationService;


    /**
     * 创建用户
     *
     * @param createUserReq 请求 BODY
     * @return com.luyang.platform.contact.rest.user.response.CreateUserResp
     * @author lu_yang
     */
    @PostMapping
    public CreateUserResp create(@RequestBody @Valid CreateUserReq createUserReq) {
        // 请求BODY 转 CMD 对象
        var createUserCommand = this.userRequestConverter.createConverter(createUserReq);
        var result = userApplicationService.create(createUserCommand);
        return new CreateUserResp(result.userId());
    }
}
